package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.Log;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.param.CriteriaParams;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.BranchCOADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.BranchCOADataList;
import com.mbc.rmt.entities.BranchCOA;
import com.mbc.rmt.entities.BranchCOARpt;

public class BranchCOAMgr extends AbstractMgr{
	
	private BranchCOADAO branchCOADAO;
	private LogDAO logDAO;
	
	public BranchCOAMgr(){
		
	}
	public BranchCOAMgr(String sessionId){
		this.sessionId = sessionId;
	}
	
	public void create(BranchCOA entity) throws Exception{
		openConnection();
		try{
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			int rowcount =0;
			rowcount = branchCOADAO.findCount(entity.getHostBranchID());
                if(rowcount == 0 ){//save
                  for (BranchCOA detail : entity.getBranchCOAList()) {
                	  BranchCOA bcoa = new BranchCOA();
                	  bcoa.setBranchCOAID(KeyGenerator.generateKey(getConnection()));
                	  bcoa.setHostBranchID(entity.getHostBranchID());
                	  bcoa.setHostBranchName(entity.getHostBranchName());
                	  bcoa.setBranchID(detail.getBranchID());
                	  bcoa.setCurrentAccountID(detail.getCurrentAccountID());
                	  bcoa.setDepartmentID(detail.getDepartmentID());
                	  bcoa.setExchangegainCOAID(detail.getExchangegainCOAID());
                	  bcoa.setDeptID(detail.getDeptID());
					  branchCOADAO.create(bcoa);
					  
					  Log l_logentity = new Log();
					  l_logentity.setTablename("BranchCOA");
					  l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					  l_logentity.setKey(bcoa.getBranchCOAID());
					  l_logentity.setUserid(bcoa.getUserid());
					  l_logentity.setLogstatus(Log.__SAVE_DATA);
					  logDAO.getData(l_logentity);
				  }
			   }else{ //edit
				   ArrayList<BranchCOA> l_entity = branchCOADAO.find(entity.getHostBranchID());
				   Log l_logentity = new Log();
				   for(int i=0; i<l_entity.size(); i++){
					
					  l_logentity.setTablename("BranchCOA");
					  l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					  l_logentity.setKey(l_entity.get(i).getBranchCOAID());
					  l_logentity.setUserid(entity.getUserid());
					  l_logentity.setLogstatus(Log.__DELETE_DATA);
					  logDAO.getData(l_logentity);
				   }
					  
					branchCOADAO.delete(entity);
					for (BranchCOA branchcoa : entity.getBranchCOAList()) {
						BranchCOA bcoa = new BranchCOA();
						bcoa.setBranchCOAID(KeyGenerator.generateKey(getConnection()));
						bcoa.setHostBranchID(entity.getHostBranchID());
						bcoa.setHostBranchName(entity.getHostBranchName());
						bcoa.setBranchID(branchcoa.getBranchID());
						bcoa.setCurrentAccountID(branchcoa.getCurrentAccountID());
						bcoa.setDepartmentID(branchcoa.getDepartmentID());
						bcoa.setExchangegainCOAID(branchcoa.getExchangegainCOAID());
						bcoa.setDeptID(branchcoa.getDeptID());
						branchCOADAO.create(bcoa);
						
						l_logentity.setTablename("BranchCOA");
						  l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
						  l_logentity.setKey(bcoa.getBranchCOAID());
						  l_logentity.setUserid(bcoa.getUserid());
						  l_logentity.setLogstatus(Log.__SAVE_DATA);
						  logDAO.getData(l_logentity);
						}
				}
			
		}catch(SQLException e){
			throw e;
		}finally{
			closeConnection();
		}
	}
	
	public void delete(BranchCOA entity) throws Exception {
		openConnection();
		try {
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			
			ArrayList<BranchCOA> l_entity  = branchCOADAO.find(entity.getHostBranchID());
			
			for(int i = 0; i < l_entity.size(); i++) {
				Log l_logentity = new Log();
				l_logentity.setTablename("BranchCOA");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(l_entity.get(i).getBranchCOAID());
				l_logentity.setUserid(entity.getUserid());
				l_logentity.setLogstatus(Log.__DELETE_DATA);
				logDAO.getData(l_logentity);
			}
			
			
			branchCOADAO.delete(entity);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public BranchCOADataList findBranchCOA(CriteriaParams cri)
			throws Exception {
		openConnection();
		try {
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			return branchCOADAO.findBranchCOAs(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public BranchCOA findByBranchCOA(String code, long branchId)throws Exception{
		openConnection();
		try {
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			return branchCOADAO.findByBranchCOA(code, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public BranchCOADataList findBranchCOA(long branchid)
			throws Exception {
		openConnection();
		try {
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			return branchCOADAO.findBranchCOAs(branchid);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	public JasperPrint prepareBranchCOAReport(BranchCOARpt entity) throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(branchCOADAO.prepareBranchCOAReport(entity));
			 JasperReport l_jsRpt =(JasperReport)JRLoader.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/BranchCOA.jasper");
			 HashMap<String, Object> l_params = new HashMap<String, Object>();
				l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
	
	public BranchCOA find(long key) throws Exception {
		openConnection();
		try{
			BranchCOA branchCOA = new BranchCOA();
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			ArrayList<BranchCOA> l_grid = new ArrayList<BranchCOA>();
			l_grid = branchCOADAO.find(key);
			branchCOA.setBranchCOAList(l_grid);
			return branchCOA;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public BranchCOA findByHostBranchID(long defaultBranchKey, long branchKey) throws Exception {
		openConnection();
		try{
			branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(dbManager.getConnection());
			return branchCOADAO.findByHostBranchID(defaultBranchKey, branchKey);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
}
